/* eslint-disable import/consistent-type-specifier-style */
/* eslint-disable camelcase */
import { Lang, parse } from "@ast-grep/napi";
import type { LoaderDefinitionFunction } from "@rspack/core";

const INLINE_COMMENT = "__INLINE__";
const NO_SIDE_EFFECTS_COMMENT = "__NO_SIDE_EFFECTS__";
const helper__inComment = <T extends string>(str: T) =>
  [
    /** section comment */ `\\/\\*\\s*[#@]${str}\\s*\\*\\/`,
    /** line comment */ `\\/\\/\\s*[#@]${str}\\W?`,
  ] /** dev-only */ as const;

export const INLINE_REG = helper__inComment(INLINE_COMMENT).map(
  (c) => new RegExp(c),
);
export const NO_SIDE_EFFECTS_REG = helper__inComment(
  NO_SIDE_EFFECTS_COMMENT,
).map((c) => new RegExp(c));

// TemplateString 的缺陷之一是无法流式编译
const transform: LoaderDefinitionFunction = function (source, sourceMap) {
  const callback = this.callback();

  const ast = parse(Lang.JavaScript, source); // 1. parse the source
  const root = ast.root(); // 2. get the root
  const htmlCall = root.find("html`$A`"); // 3. find the node
  // ! fail to split TemplateLiteral
  const htmlString = root.find("html`$$$`"); // 3. find the node
};
